#include "BehaviorAlignment.h"
#include "BoidBase.h"

//! executes behavior.
Ogre::Vector3 BehaviorAlignment::execute(const BoidBase* boid)
{
	Ogre::Vector3 desiredVector(0.0f, 0.0f, 0.0f);

	const BoidList& neighbors = boid->getNeighbors();
	BoidList::const_iterator iter = neighbors.begin();
	int count = 0;

	while (iter != neighbors.end())
	{
		if (mMaxRadius <= 0.0f || boid->isInNeighborhood(*iter, mMinRadius, mMaxRadius, mCosMaxAngle))
		{
			desiredVector += (*iter)->getForwardVector();
			count++;
		}

		iter++;
	}

	if (count)
	{
		desiredVector /= count;
		desiredVector -= boid->getForwardVector();
		desiredVector.normalise();
	}

	return desiredVector;
}
